package com.example.config;

import lombok.Data;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;

/** ElasticsearchRestTemplate基于RestHighLevelClient客户端的。
 * 需要自定义配置类，继承AbstractElasticsearchConfiguration，
 * 并实现elasticsearchClient()抽象方法，创建RestHighLevelClient对象。
 * @author 24412
 */
@ConfigurationProperties(prefix = "elasticsearch")
@Configuration
@Data
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration{
     /**
     * 设置为static方便后面调用
     */
    public static RestHighLevelClient restHighLevelClient;
    private String host ;
    private Integer port ;
    /**
     * 重写父类方法
     */
    @Override
    public RestHighLevelClient elasticsearchClient() {
        RestClientBuilder builder = RestClient.builder(new HttpHost(host, port));
        restHighLevelClient = new RestHighLevelClient(builder);
        return restHighLevelClient;
    }
}
